import os
import wx
from sdlThread import SDLThread

global pygame # when we import it, let's keep its proper name!
global pygame_init_flag
pygame_init_flag = False

class SDLPanel(wx.Panel):
    def __init__(self,parent,ID,tplSize):
        global pygame
        global pygame_init_flag
        wx.Panel.__init__(self, parent, ID, size=tplSize)
        self.Fit()
        os.environ['SDL_WINDOWID'] = str(self.GetHandle())

        os.environ['SDL_VIDEODRIVER'] = 'windib'
        #here is where things change if pygame has already been initialized
        #we need to do so again

        if pygame_init_flag:
            #call pygame.init() on subsaquent windows
            pygame.init()
        else:
            #import if this is the first time
            import pygame
        pygame_init_flag = True #make sure we know that pygame has been imported
        pygame.display.init()
        self.Show(True)
        window = pygame.display.set_mode(tplSize)
        self.thread = SDLThread(window)
        self.thread.Start()

    def __del__(self):
        self.thread.Stop()
        print "thread stoped"
        #very important line, this makes sure that pygame exits before we
        #reinitialize it other wise we get errors
        pygame.quit()